[T4Scaffolding.Scaffolder(Description = "Enter a description of EditViewModel here")][CmdletBinding()]
param(        
    [parameter(Mandatory = $true)][string]$ModelType,
    
    [string]$Project, # this must remain here because of T4Scaffolding logic
    
    [parameter(Mandatory = $true)][string]$FrontendProject,
    [parameter(Mandatory = $true)][string]$CommonProject,
    [parameter(Mandatory = $true)][string]$BackendProject,
    
	[string]$CodeLanguage,
	[string[]]$TemplateFolders,
	[switch]$Force = $false
)

# find type of model class
$foundModelType = Get-ProjectType $ModelType -Project $CommonProject -BlockUi
if (!$foundModelType) { return }

# name and its plural of model class
$modelTypeName = $foundModelType.Name
$modelTypePluralized = Get-PluralizedWord $modelTypeName

# name of primary key 
$primaryKey = Get-PrimaryKey $foundModelType.FullName -Project $CommonProject -ErrorIfNotFound
if (!$primaryKey) { return }

#scaffold Get message
$getMessageName = "Get"+$modelType;
$outputPath = Join-Path ("Messages"+$modelTypeName) $getMessageName
$getModel = @{
    Namespace = (Get-Project $CommonProject).Properties.Item("DefaultNamespace").Value; 
    Group = $modelTypeName; 
    MessageName = $getMessageName;
    ModelType = $foundModelType;
    ModelTypePluralized = $modelTypePluralized;
    PrimaryKey = $primaryKey;
    }

Add-ProjectItemViaTemplate $outputPath -Template GetMessageTemplate `
	-Model $getModel `
	-SuccessMessage "Added Get message output at {0}" `
	-TemplateFolders $TemplateFolders -Project $CommonProject -CodeLanguage $CodeLanguage -Force:$Force

#scaffold Get handler
Scaffold ZeroScaffolding.MessageHandler -MessageType $getMessageName -HandlerName $modelTypeName `
    -HandlerTemplate "GetHandlerTemplate" `
    -HandlerModel @{ModelType = $modelTypeName; ModelTypePluralized = $modelTypePluralized; ContextType = "ModelContext"} `
    -CommonProject $CommonProject `
    -BackendProject $BackendProject `
    -OverrideTemplateFolders $TemplateFolders

#scaffold Save message
$saveMessageName = "Save"+$modelType;
$outputPath = Join-Path ("Messages"+$modelTypeName) $saveMessageName
$saveModel = @{
    Namespace = (Get-Project $CommonProject).Properties.Item("DefaultNamespace").Value; 
    Group = $modelTypeName; 
    MessageName = $saveMessageName;
    ModelType = $foundModelType;
    ModelTypePluralized = $modelTypePluralized;
    PrimaryKey = $primaryKey;
    }

Add-ProjectItemViaTemplate $outputPath -Template SaveMessageTemplate `
	-Model $saveModel `
	-SuccessMessage "Added Save message output at {0}" `
	-TemplateFolders $TemplateFolders -Project $CommonProject -CodeLanguage $CodeLanguage -Force:$Force

#scaffold Save handler
Scaffold ZeroScaffolding.MessageHandler -MessageType $saveMessageName -HandlerName $modelTypeName `
    -HandlerTemplate "SaveHandlerTemplate" `
    -HandlerModel @{ModelType = $modelTypeName; ModelTypePluralized = $modelTypePluralized; ContextType = "ModelContext"; PrimaryKey = $primaryKey } `
    -CommonProject $CommonProject `
    -BackendProject $BackendProject `
    -OverrideTemplateFolders $TemplateFolders

$outputPath = Join-Path "ViewModels" ($modelTypeName +"EditViewModel")  # The filename extension will be added based on the template's <#@ Output Extension="..." #> directive
$namespace = (Get-Project $FrontendProject).Properties.Item("DefaultNamespace").Value
$commonNamespace = (Get-Project $CommonProject).Properties.Item("DefaultNamespace").Value

$model = @{ 
    Namespace = $namespace;     
    CommonNamespace = $commonNamespace;
    ModelType = $foundModelType ;    
    PrimaryKey = [string]$primaryKey;
    ModelTypePluralized = [string]$modelTypePluralized; 
    
    GetMessage = $getMessageName;
    SaveMessage = $saveMessageName;
    };

Add-ProjectItemViaTemplate $outputPath -Template EditViewModelTemplate `
	-Model $model `
	-SuccessMessage "Added ListViewModel output at {0}" `
	-TemplateFolders $TemplateFolders -Project $FrontendProject -CodeLanguage $CodeLanguage -Force:$Force    